
package taskman.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import taskman.Task;
import taskman.xml.XmlHelper;


public class TasksManagementWindow extends JFrame
{

	private static final long	serialVersionUID	= 1L;
	private JPanel				contentPane;
	private JTable				table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					TasksManagementWindow frame = new TasksManagementWindow();
					frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TasksManagementWindow()
	{
		setTitle("Task Management");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JLabel lblTaskManagement = new JLabel("Task Management");
		contentPane.add(lblTaskManagement, BorderLayout.NORTH);

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		// Creating the table
		table = new JTable();
		DefaultTableModel model = new DefaultTableModel();

		// Adding the header of each column
		model.addColumn("Id");
		model.addColumn("Title");
		model.addColumn("Description");
		model.addColumn("Duration");
		model.addColumn("Delivarable");
		model.addColumn("Deadline");
		model.addColumn("Person Id");
		model.addColumn("Parent Task ID");
		model.addColumn("Percent Complete");
		model.addColumn("Number Of Days Elapsed");
		model.addColumn("Start Date");
		
		// Getting all the tasks from the XmlHelper, inorder to put them in the
		// table
		for (Task t : XmlHelper.loadAllTasks())
		{
			model.addRow(new Object[]
			{ t.getId(), t.getTitle(), t.getDescription(), t.getDuration(),
					t.getDeliverable(), t.getDeadline(), t.getPersonId(), t.getParentTaskId(), 
					t.getPercentageComplete(), t.getNumberOfDaysElapsed(), t.getStartDate()});
		}

		table.setModel(model);
		table.getColumnModel().getColumn(0).setResizable(true);
		table.getColumnModel().getColumn(1).setResizable(true);
		table.getColumnModel().getColumn(2).setResizable(true);
		table.getColumnModel().getColumn(3).setResizable(true);
		table.getColumnModel().getColumn(4).setResizable(true);
		table.getColumnModel().getColumn(5).setResizable(true);
		table.getColumnModel().getColumn(6).setResizable(true);
		table.getColumnModel().getColumn(7).setResizable(true);
		table.getColumnModel().getColumn(8).setResizable(true);
		table.getColumnModel().getColumn(9).setResizable(true);
		table.getColumnModel().getColumn(10).setResizable(true);

		scrollPane.setViewportView(table);

	}

}
